package com.fesine.other;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/2/13 14:02
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/2/13 14:02
 */
public class ThreadPoolExecutorPractice {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        for (int i = 0; i < 15; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("线程池中的线程数量：" + executor.getPoolSize() + ",队列中等待执行的任务数目：" +
                    executor.getQueue().size()+",已执行完毕的任务数目："+executor.getCompletedTaskCount());
        }
        executor.shutdown();
    }

}

class MyTask implements Runnable{

    private int taskNum;
    public MyTask(int taskNum){
        this.taskNum=taskNum;
    }

    @Override
    public void run() {
        System.out.println("正在执行的task " + taskNum);
        try {
            Thread.currentThread().sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task "+taskNum+"执行完毕。");
    }
}
